import { message } from 'antd';
import { fetchBookCatelog } from '@/service/borrowBook';
import { useRequest } from 'ahooks';
import { injectEnumByKey } from '@/utils/form/injectCatelog';
import { ProColumnType } from '@ant-design/pro-table';

export default function useAutoInjectCatelog<T>(columns: ProColumnType<T>[]) {
    const { data, loading } = useRequest(
        async () => {
            const res = await fetchBookCatelog();

            if (res.code !== 200) {
                message.error(res.msg || '服务器错误');
                return;
            }
            return res.data;
        },
        { manual: false },
    );
    const newColumns = injectEnumByKey(
        columns,
        { cate_id: data },
        ['cate_id'],
        {
            key: 'cate_name',
            value: 'id',
        },
    );

    return {
        data,
        loading,
        columns: newColumns,
    };
}
